import bagel.util.Rectangle;
import java.util.List;
import bagel.util.Point;


public class CollisionManager {
    private List<Platform> platforms;
    private List<Ladder> ladders;
    private List<Enemy> barrels;
    private Enemy kong;
    private Hammer hammer;

    public CollisionManager(List<Platform> platforms) {
        this.platforms = platforms;
    }

    public CollisionManager(List<Platform> platforms,List<Ladder> ladders,List<Enemy> barrels,Enemy kong,Hammer hammer) {
        this.platforms = platforms;
        this.ladders = ladders;
        this.barrels = barrels;
        this.kong = kong;
        this.hammer=hammer;
    }

    public Point OnPlatform(Point marioPos,double width,double height){
        Rectangle mBox = new Rectangle(marioPos.x-width/2,marioPos.y-height/2,width,height);
        for(Platform p:platforms){
            Rectangle pBox = p.getBoundingBox();
            if(pBox.intersects(mBox) && marioPos.x < pBox.right() && marioPos.x > pBox.left() && mBox.bottom() < pBox.bottom()){
                marioPos = new Point(marioPos.x,p.getPosition().y-p.getHeight()/2-height/2);
                return marioPos;
            }
        }
        return null;
    }

    public boolean OnLadder(Point marioPos,double width,double height){
        Rectangle mBox = new Rectangle(marioPos.x-width/2,marioPos.y-height/2,width,height);
        for(Ladder l:ladders){
            Rectangle lBox = l.getBoundingBox();
            if(lBox.intersects(mBox) && marioPos.x < lBox.right() && marioPos.x > lBox.left() && mBox.bottom() <= lBox.bottom()){
                return true;
            }
        }
        return false;
    }

    public Enemy OnBarrels(Point marioPos,double width,double height){
        Rectangle mBox = new Rectangle(marioPos.x-width/2,marioPos.y-height/2,width,height);
        for(Enemy e:barrels){
            Rectangle eBox = e.getBoundingBox();
            if(eBox.intersects(mBox)){
                return e;
            }
        }
        return null;
    }

    public boolean OnKong(Point marioPos,double width,double height){
        Rectangle mBox = new Rectangle(marioPos.x-width/2,marioPos.y-height/2,width,height);
       
        Rectangle kBox = kong.getBoundingBox();
        if(kBox.intersects(mBox)){
            return true;
        }
        
        return false;
    }

    public Point getBarrelCount(Point marioPosition, double width,double height){
        int left=0,right=0;
        for(Enemy e:barrels){
            Rectangle eBox = e.getBoundingBox();
            if(Math.abs(eBox.bottom() - marioPosition.y-height/2)<1){
                if(e.getPosition().x < marioPosition.x){
                    left++;
                }else{
                    right++;
                }
                
            }
        }
        //System.out.printf("barrel left=%d,right=%d\n",left,right);
        Point count = new Point(left,right);
        return count;
    }
    
    public boolean OnHammer(Point marioPos,double width,double height){
        Rectangle mBox = new Rectangle(marioPos.x-width/2,marioPos.y-height/2,width,height);
       
        Rectangle hBox = hammer.getBoundingBox();
        if(hBox.intersects(mBox)){
            return true;
        }
        
        return false;
    }
    public void removeBarrel(Enemy barrel) {
        barrels.remove(barrel);
    }
}